﻿using MCMesServer.DBFunctions.DBFuncs;
using MCMesServer.DBFunctions.TransformFuncs;
using MCMesServer.Model;
using MCMesServer.NetConnectFunc;
using MCMesServer.PublicLogInfo;
using MesDAL.DTO.ProcessSheet;
using MesDAL.DTO.SelectReport;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.ServerReceiveFunctions.SelectProcessSheetAndPlan
{
    public class SelectProcessSheetAndPlanHandlerFunc
    {
        public static void HandleReceiveMsg(ServerModel model, IntPtr connId)
        {
            try
            {
                switch (model.Operation)
                {
                    case Operations.SelectUnFinishWorkpieceProcessAndPlan:
                        ReceiveSelectUnFinishWorkpieceProcessAndPlan(model, connId);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage($"[{model.Step}]发生接收错误，错误信息内容：{model.Message}", ex.ToString());
            }
        }

        /// <summary>
        /// 检索查询当前未完成工件的工艺单以及排产单
        /// </summary>
        /// <param name="model"></param>
        /// <param name="connId"></param>
        public async static void ReceiveSelectUnFinishWorkpieceProcessAndPlan(ServerModel model, IntPtr connId)
        {
            try
            {
                List<ProcessSheetDTO> processList = await ProcessSheetDBFunc.SelectIncompleteProcessSheet();
                List<UnfinishWorkpieceDTO> workList = new List<UnfinishWorkpieceDTO>();
                foreach (var item in processList)
                {
                    ProduceplanDTO dto = await ProduceplanDBFunc.SelectProduceplanByProcessSheet(item);
                    int index = -1;
                    for (int i = 0; i < workList.Count; i++)
                    {
                        if (workList[i].workpiece.id == item.workpiece.id)
                        {
                            index = i;
                        }
                    }
                    if (index < 0)
                    {
                        UnfinishWorkpieceDTO workDto = new UnfinishWorkpieceDTO()
                        {
                            workpiece = item.workpiece,
                            process_list = new List<ProcessSheetDTO>(),
                            produce_list = new List<ProduceplanDTO>(),
                        };
                        workDto.process_list.Add(item);
                        if (dto is null)
                        {
                            workList.Add(workDto);
                            continue;
                        }
                        else
                        {
                            workDto.produce_list.Add(dto);
                        }
                        workList.Add(workDto);
                    }
                    else
                    {
                        workList[index].process_list.Add(item);
                        if (dto is null)
                        {
                            continue;
                        }
                        else
                        {
                            workList[index].produce_list.Add(dto);
                        }
                    }
                }
                ServerModel serverModel = new ServerModel()
                {
                    MachineNum = model.MachineNum,
                    Step = model.Step,
                    Operation = model.Operation,
                    Message = TransformModelFunc<List<UnfinishWorkpieceDTO>>.TransformModel2StringFunc(workList)
                };
                CommonConnectFunc.SendInterServerModelFunc(connId, serverModel);
            }
            catch (Exception ex)
            {
                CommonConnectFunc.SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
                return;
            }
        }
    }
}
